<?php
class Zendvn_Menu{
	
	protected $_id;
	
	protected $_menuDb = array();
	
	/*
	 * $options['id']
	 */
	public function __construct($options = array()){
		
	}
	
	public function setGroup($id = null){
		$this->_id = $id;
	}
	
	public function getGroup(){
		return $this->_id;
	}
	
	public function loadModule(){
		$filename = CONFIG_PATH . '/modules.ini';
		$config = new Zend_Config_Ini($filename,'default');
		
	}
	
	public function getDb($options = null){
				
		if(!empty($this->_id)){
			$db = Zend_Db_Table::getDefaultAdapter();
						
			$select = $db->select()
					 ->from('menu_items')
					 ->where('menu_group_id = ?',$this->_id)
					 ->where('status = ?','active')
					 ->order('lft ASC');
			$this->_menuDb = $db->fetchAll($select);
		}
		
		return $this->_menuDb;
		
	}
	
	public function createNavigationArray(){
		$newArray = array();
		$this->recursiveNavigation($this->_menuDb,0,1,$newArray);
		
		return $newArray;
	}
		
	private function recursiveNavigation($sourceArr,$parents = 0,$level = 1,&$resultArr){
		if(count($sourceArr)>0){
			foreach ($sourceArr as $key => $value){
				if($value['parents'] == $parents){
					$tmp = array(
								'id' => $value['id'],
								'label' => $value['name'],
			                	'action' =>  $value['action'],
			                	'controller' =>  $value['controller'],
			                	'module' =>  $value['module']
								); 
					$resultArr[] = $tmp; 								
					unset($sourceArr[$key]);
				}
			}
		}
	}
	
	private function recursive($sourceArr,$parents = 0,$level = 1,&$resultArr){
		if(count($sourceArr)>0){
			foreach ($sourceArr as $key => $value){
				if($value['parents'] == $parents){
					$value['level'] = $level;
					$resultArr[] = $value;
					$newParents = $value['id'];
					unset($sourceArr[$key]);
					$this->recursive($sourceArr,$newParents, $level + 1,$resultArr);
				}
			}
		}
	}
}